//
// OpenTemplate
// Copyright (c) 2006
// by OpenArrow Software ( http://www.openarrow.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;
using OpenArrow.Templating.Properties;

namespace OpenArrow.Templating
{
    /// <summary>
    /// Utility class containing helper functions for string manipulation
    /// </summary>
    public static class StringUtils
    {
        /// <summary>
        /// Strips text from the left and right of the string
        /// </summary>
        /// <param name="s">The string to strip text from</param>
        /// <param name="fromLeft">The number of characters from the left to remove</param>
        /// <param name="fromRight">The number of characters from the right to remove</param>
        /// <returns>The string with the text removed</returns>
        /// <exception cref="ArgumentException"><paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/></exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")]
        public static string StripOuter(string s, int fromLeft, int fromRight)
        {
            if (String.IsNullOrEmpty(s))
                throw new ArgumentException(String.Format(Resources.Exception_ArgIsNullOrEmpty, "s"), "s");

            return s.Substring(fromLeft, s.Length - (fromLeft + fromRight));
        }
        
        /// <summary>
        /// Takes a quoted value from the string, assuming that the first character is the opening quote (' or ") and
        /// the last character is the closing quote (' or ", depending on the opening quote).
        /// </summary>
        /// <remarks>
        /// This method is purely for verification of the quote characters on either side and for removing the quote characters
        /// from the string
        /// </remarks>
        /// <param name="s">The string to take a quoted value from</param>
        /// <returns>The string with quotes removed</returns>
        /// <exception cref="FormatException">The string did not start with ' or "</exception>
        /// <exception cref="FormatException">The quote characters did not match (i.e. started with ' but ended with " or vice-versa)</exception>
        /// <exception cref="ArgumentException"><paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/></exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")]
        public static string TakeQuotedValue(string s)
        {
            if (String.IsNullOrEmpty(s))
                throw new ArgumentException(String.Format(Resources.Exception_ArgIsNullOrEmpty, "s"), "s");

            char end = s[0];
            if ((end != '"') && (end != '\''))
                throw new FormatException(Resources.Exception_InvalidQuoteCharacter);
            if (s.StartsWith(end.ToString()) && s.EndsWith(end.ToString()))
                return s.Substring(1, s.Length - 2);
            else
                throw new FormatException(Resources.Exception_QuoteMismatch);
        }
    }
}
